package org.example.zpqy.controll;

import org.example.zpqy.bean.WeatherResponse;
import org.example.zpqy.service.PlantWeatherAdvice;
import org.example.zpqy.service.WeatherService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/weather")
public class WeatherController {
    private final WeatherService weatherService;
    private final PlantWeatherAdvice plantWeatherAdvice;

    public WeatherController(WeatherService weatherService, PlantWeatherAdvice plantWeatherAdvice) {
        this.weatherService = weatherService;
        this.plantWeatherAdvice = plantWeatherAdvice;
    }

    /**
     * 获取指定城市和日期的天气
     * @param city 城市编码（如：110000 表示北京）
     * @param date 日期（格式：yyyy-MM-dd）
     * @return 天气信息
     */
    @GetMapping
    public ResponseEntity<WeatherResponse> getWeather(
            @RequestParam String city,
            @RequestParam String date) {

        WeatherResponse weather = weatherService.getWeatherByDate(city, date);
        if (weather != null) {
            return ResponseEntity.ok(weather);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }

    @GetMapping("/advice")
    public ResponseEntity<Map<String, Object>> advice(
            @RequestParam String weatherInfo,
            @RequestParam String plant) {
        String adviceInfo  = plantWeatherAdvice.getAiInfo(weatherInfo,plant);
        Map<String, Object> result = new HashMap<>();
        result.put("advice", adviceInfo); // ✅ 固定返回 advice 字段
        if (adviceInfo != null) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }

}
